<?php
namespace app\beautiful\helper\payment;
use app\common\helper\payment\PaymentBaseHelper;
use app\common\logic\SmsLogic;
use app\beautiful\logic\LogsLogic;
use app\beautiful\model\member\MemberCouponModel;
use app\beautiful\logic\MemberWalletLogic;
use app\common\model\ShopGmvLogsModel;
use app\beautiful\logic\PartnerLogic;
use app\beautiful\logic\PinTuanLogic;
use app\beautiful\model\order\PinTuanOrderModel;
use app\beautiful\model\setting\SettingSmsModel;
use think\facade\Db;
//每个子类只需要专注于自己的 respond 逻辑
class PaymentPintuanOrderHelper extends PaymentBaseHelper{
    protected  $moudel = 'beautiful';
    protected  $orderKey = 'mpt'; //给一个KEY增大吞吐量
    protected  $respondAction = 'pintuanMallRespond';
    
    public function respondCallback($orderId,$payInfo,$fail){
        
        if( null === $order = PinTuanOrderModel::find($orderId)){
            return false;
        }
        if($order->is_paid == 1){
            return false;
        }
        if($order->status != 1){ //不是在支付状态
            return false;
        }
        $isFail = false;
        Db::startTrans();
        try{ //成团失败会报错
            $order->is_paid = 1;
            $order->pay_time = time();
            $order->status = 2;
            $order->pay_type = 'weixin';
            $order->pay_info = $payInfo;
            $order->save();
            
            if(!empty($order->member_coupon_id)){ //优惠券的使用
                if(null !== $myCoupon = MemberCouponModel::find($order->member_coupon_id)){
                    if($myCoupon->status == 0){
                        $myCoupon->status = 1;
                        $myCoupon->save();
                        LogsLogic::coupon($order->shop_id, $order->member_id, -1, 12, $order->member_coupon_id, '', 0);
                    }
                }
            }
            $MemberWalletLogic = new MemberWalletLogic($order->shop_id, $order->member_id);
            if(!empty($order->use_integral)){ //使用积分为商城支付
               $MemberWalletLogic->useIntegral($order->use_integral, 9);
            }
            $needPay = $order->need_pay;
            
            //插入日志
            $shop_gmv_logs = new ShopGmvLogsModel();
            $shop_gmv_logs->shop_id=$order->shop_id;
            $shop_gmv_logs->balance=$needPay;
            $shop_gmv_logs->save();
            
            $params=[
                'order_id'=>$order->pin_tuan_order_id,
                'member_id'=>$order->member_id,
            ];
            $pin_tuan_logic = new PinTuanLogic($order->shop_id);
            $pin_tuan_logic->payOk($params);
            Db::commit();
        }catch(\Exception $e){
            Db::rollback();
            $isFail = true;
        }
        if($isFail == true){ //如果失败了
            try{
                $pin_tuan_logic = new PinTuanLogic($order->shop_id);
                $pin_tuan_logic->cancelPay([
                    'order_id'=>$order->pin_tuan_order_id,
                    'member_id'=>$order->member_id,
                ]);
                //退款操作
                $this->refundInTheSameWay($payInfo);
                $fail('拼团失败已退款');
            } catch (\Exception $e){
                //不做什么处理了
                $fail('拼团失败');
            }
        }else{
            //发送短信给管理员
            $sms_setting = SettingSmsModel::where([['shop_id','=',$order->shop_id]])->find();
            if(null !== $sms_setting){
                if($sms_setting->order_notice_admin==1){
                    $sms_logic= new SmsLogic();
                    $params=[
                        'money'=>$needPay,
                    ];
                    $sms_logic->send($order->shop_id,$sms_setting->boss_mobiles,$params,'orderNoticeAdmin');
                }
            }
        }
        
    }
}